package com.bong.panpan.bongcharttools.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.custom.MyUtil;
import com.bong.panpan.bongcharttools.custom.UserInfoRecord;

/**
 * Created by panpan on 2017/11/7.
 */

public class MyDialog {
    private AlertDialog alertDialog;
    private Listener listener;
    private UserInfoRecord record;

    public MyDialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("参考数据");
        builder.setView(R.layout.layout_my_dialog);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                byte type = (byte) (radioGroup.getCheckedRadioButtonId() == R.id.radioButton1 ? 0 : 1);
                int highValue = Integer.valueOf(high.getText().toString());
                int lowValue = Integer.valueOf(low.getText().toString());
                record.instrumentType = type;
                record.highBloodPressure = (byte) highValue;
                record.lowBloodPressure = (byte) lowValue;
                if (listener != null) {
                    listener.OnOkClick(type, (byte) highValue, (byte) lowValue);
                }
                MyUtil.closeKeybord(context);
            }
        });
        builder.setNegativeButton("取消", null);
        alertDialog = builder.create();
        //initView();
    }

    private RadioGroup radioGroup;
    private RadioButton[] radios = new RadioButton[2];
    private EditText high, low;
    private TextView userInfo;

    private void initView() {
        if (radioGroup != null)
            return;
        radioGroup = (RadioGroup) alertDialog.findViewById(R.id.radioGroup);
        radios[0] = (RadioButton) alertDialog.findViewById(R.id.radioButton1);
        radios[1] = (RadioButton) alertDialog.findViewById(R.id.radioButton2);
        high = (EditText) alertDialog.findViewById(R.id.editText1);
        low = (EditText) alertDialog.findViewById(R.id.editText2);
        userInfo = (TextView) alertDialog.findViewById(R.id.text_user_info);
        radioGroup.setOnCheckedChangeListener(checkListener);
        high.addTextChangedListener(watcher);
        low.addTextChangedListener(watcher);
    }

    public void show(UserInfoRecord record, Listener listener) {
        this.record = record;
        this.listener = listener;
        alertDialog.show();
        initView();
        radios[record.instrumentType].setChecked(true);
        //radioGroup.clearCheck();
        high.setText("" + MyUtil.byte2Int(record.highBloodPressure));
        low.setText("" + MyUtil.byte2Int(record.lowBloodPressure));
        userInfo.setText(String.format("%s %s:\n性别:%s，年龄:%s，身高(cm):%s，体重(Kg):%s",
                record.editText.get(0),
                record.editText.get(1),
                record.radioChecks.get(0) ? "男" : "女",
                record.editText.get(4),
                record.editText.get(2),
                record.editText.get(3)));
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }

    public interface Listener {
        void OnOkClick(byte type, byte high, byte low);
    }

    private boolean isOK() {
        if (radioGroup.getCheckedRadioButtonId() == -1)
            return false;
        if (high.getText().toString().equals(""))
            return false;
        if (low.getText().toString().equals(""))
            return false;
        return true;
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (isOK()) {
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
        }
    };
    private RadioGroup.OnCheckedChangeListener checkListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (isOK()) {
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
        }
    };
}
